﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串必须以给定的值开头, 示例:
/// <code>
/// [VMustStarts("中国")]
/// public string Province { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustStartsAttribute(string value, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public string Value { get; set; } = value;
    public bool IgnoreCase { get; } = ignoreCase;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustStartsWithInternal(ctx, ctx.ModelValue?.ToString(), Value, IgnoreCase, ErrorMessage);
    }
}

/// <summary>
/// 如果buweinull, 字符串必须以给定的值开头, 示例:
/// <code>
/// [VMustStartsIfNotNull("中国")]
/// public string Province { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustStartsIfNotNullAttribute(string value, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public string Value { get; set; } = value;
    public bool IgnoreCase { get; } = ignoreCase;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustStartsWithInternal(ctx, ctx.ModelValue?.ToString(), Value, IgnoreCase, ErrorMessage);
    }
}
